﻿
using DataSync.Web.Core;

namespace DataSync.Application
{
    public class SystemService : ISystemService, ITransient
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public SystemService(IHttpContextAccessor httpContextAccessor)
        {
            this._httpContextAccessor = httpContextAccessor;
        }
        public string GetDescription()
        {
            return "";
        }

        public async Task<TokenOutput> GetToken()
        {
            // 生成Token令牌
            var tokenExpire = 10800;
            var accessToken = JWTEncryption.Encrypt(new Dictionary<string, object>
            {
            { Const.UserId, "1" },
            { Const.Account, App.GetConfig<string>("LoginUser:Account") },
            { Const.Password, App.GetConfig<string>("LoginUser:Password") },

            }, tokenExpire);

            // 生成刷新Token令牌
            var refreshTokenExpire = 20160;
            var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, refreshTokenExpire);

            // 设置响应报文头
            _httpContextAccessor.HttpContext.SetTokensOfResponseHeaders(accessToken, refreshToken);

            // Swagger Knife4UI-AfterScript登录脚本
            // ke.global.setAllHeader('Authorization', 'Bearer ' + ke.response.headers['access-token']);
            return await Task.Run(() => new TokenOutput
            {
                AccessToken = accessToken,
                RefreshToken = refreshToken
            });

        }
    }
}
